import $H from '@/common/request.js'
export default {
  namespaced: true,
  state() {
    return {
      addressList: [],
      checkedIdx: 0,
    }
  },
  actions: {
    addressGetAct(ctx,userId) {
      $H.get(`/1.1/classes/address?where={"userid":"${userId}"}`).then((res) => {
        ctx.commit('getAddresList',res.data.results)
      })
    },
    addressAddAct(ctx, addObj) {
      $H.post('/1.1/classes/address', addObj).then((res) => {
        ctx.commit('addressAdd',addObj)
      })
    },
    updateAddressAct(ctx,parm) {
      console.log("parm=========",parm);
      const {id,val} = parm
      $H.put(`/1.1/classes/address/${id}`,val).then((res) => {
        // console.log('addreslist=========',res);
      })
    },
  },
  mutations: {
    getAddresList(state,addArr) {
      state.addressList = addArr
    },
    addressAdd(state, val) {
      state.addressList.push(val)
    },
    addressDefaultMut(state, id) {
      state.addressList.forEach((item, idx) => {
        if (idx !== id) {
          item.isdefault = false
        }else {
          item.isdefault = true
        }
      })
    },
    deleteAddress(state,index){ // 删除地址
    			state.addressList.splice(index,1)
    		},
  },
  getters: {
    orderAddress(state) {
      const resData = state.addressList.find((item) => {
        return item.isdefault == true
      })
      return resData || {}
    }
  }
}
